package cn.txy.article.controller;

import cn.txy.article.domain.ArticleType;
import cn.txy.article.query.ArticleTypeQuery;
import cn.txy.article.service.ArticleTypeService;
import cn.txy.config.R;
import cn.txy.config.RedisConstant;
import cn.txy.system.annotation.PreAuthorize;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api("ArticleType相关的接口")
@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {
    @Autowired
    private ArticleTypeService articleTypeService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping("/{id}")
    @PreAuthorize(name = "根据ID查询文章类型",sn="articleType:getOne",descs = "根据ID查询文章类型")
    public R findById(@PathVariable("id")Long id){
        return R.ok(articleTypeService.findById(id));
    }

    @GetMapping
    @PreAuthorize(name = "查询文章",sn="articleType:getAll",descs = "查询文章")
    public R findAll(){
        return R.ok(articleTypeService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据ID删除文章",sn="articleType:delOne",descs = "根据ID删除文章")
    public R deleteById(@PathVariable("id")Long id){
        redisTemplate.delete(RedisConstant.ARTICLE_TYPE_TREE);
        articleTypeService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(name = "批量删除文章",sn="articleType:deleteAll",descs = "批量删除文章")
    public R batchDelete(@RequestBody List<Long> ids){
        redisTemplate.delete(RedisConstant.ARTICLE_TYPE_TREE);
        articleTypeService.batchDelete(ids);
        return R.ok();
    }

    @ApiOperation("添加和修改")
    @PostMapping
    @PreAuthorize(name = "获取点赞数",sn="articleType:addAndUpdate",descs = "获取点赞数")
    public R add(@RequestBody ArticleType articleType){
//        redisTemplate.delete(RedisConstant.ARTICLE_TYPE_TREE);
        if(articleType.getId() == null){
            articleType.setCreateTime(new Date());
            articleTypeService.add(articleType);
        }else {
            articleType.setUpdateTime(new Date());
            articleTypeService.update(articleType);
        }
        return R.ok();
    }
    @PostMapping("/page")
    @PreAuthorize(name = "分页查询文章",sn="articleType:page",descs = "分页查询文章")
    public R getPage(@RequestBody ArticleTypeQuery query){
        PageHelper.startPage(query.getPage(),query.getSize());
        List<ArticleType> articleTypes = articleTypeService.queryPage(query);
        PageInfo<ArticleType> pageInfo = new PageInfo<>(articleTypes);
        return R.ok(pageInfo);
    }

    @GetMapping("/articleTypeTree")
    @PreAuthorize(name = "文章类型树",sn="articleType:articleTypeTree",descs = "文章类型树")
    public R articleTypeTree(){
        return R.ok(articleTypeService.articleTypeTree());
    }

}
